import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
	{
		path: '/login',
		name: 'Login',
		component: () => import('../views/Login.vue'),
	},
	{
		path: '/home',
		name: 'Home',
		component: () => import('../views/Home.vue'),
		children: [
			{
				path: '/users',
				name: '用户',
				component: () => import('@/components/users.vue'),
			},
			{
				path: '/orders',
				name: '订单管理',
				component: () => import('@/components/orders.vue'),
			},
		],
		beforeEnter: (to, from, next) => {
			if (!sessionStorage.list) {
				alert('请先登录')
				next('/login')
			} else {
				next()
			}
		},
	},

	{
		path: '/',
		redirect: '/login',
	},
]

const router = new VueRouter({
	routes,
})

export default router
